package com.atgm.mall.gmmallclub.controller;

import com.atgm.common.utils.PageUtils;
import com.atgm.common.utils.R;
import com.atgm.mall.gmmallclub.entity.ArticleEntity;
import com.atgm.mall.gmmallclub.service.ArticleService;
import com.atgm.mall.gmmallclub.vo.resp.ArticleRespVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("gmmallclub/clubarticle")
public class ArticleController {

    @Autowired
    private ArticleService articleService;

    /**
     * 获取文章列表
     *
     * @param params
     * @return
     */
    @GetMapping("/list")
    public R getClubArticleList(@RequestParam Map<String, Object> params) {
        PageUtils pageUtils = articleService.queryPage(params);
        return R.ok().setData(pageUtils);
    }

    @GetMapping("/info/{id}")
    public R getArticleInfoById(@PathVariable("id") Long id) {
        ArticleRespVo articleRespVo = articleService.queryArticleInfoById(id);
        return R.ok().setData(articleRespVo);
    }

    /**
     * 浏览数量+1
     * @param id
     * @return
     */
    @GetMapping("/increaseViewCount/{id}")
    public R increaseViewCountById(@PathVariable("id") Long id) {
        articleService.updateViewCount(id);
        return R.ok();
    }

    /**
     * 修改点赞数量
     * @param id
     * @param count
     * @return
     */
    @GetMapping("/updateThumbsCount/{id}/{count}")
    public R updateThumbsCount(@PathVariable("id") Long id, @PathVariable("count") Integer count) {
        articleService.updateThumbsCount(id, count);
        return R.ok();
    }

    /**
     * 修改收藏数量信息
     * @param id
     * @param count
     * @return
     */
    @GetMapping("/updateCollectCount/{id}/{count}")
    public R updateCollectCount(@PathVariable("id") Long id, @PathVariable("count") Integer count) {
        articleService.updateCollectCount(id, count);
        return R.ok();
    }

    /**
     * 获取热帖
     * @return
     */
    @GetMapping("/getHotArticle")
    public R getHotArticle() {
        List<ArticleEntity> list = articleService.queryHotArticle();
        return R.ok().setData(list);
    }

    @PostMapping("/saveArticle")
    public R saveArticle(@RequestBody ArticleEntity articleEntity) {
        articleService.saveArticleInfo(articleEntity);
        return R.ok();
    }

}
